#!/usr/bin/env python

import BugleChecks

Import('envs', 'targets', 'aspects')

if 'debugger' in aspects['parts']:
    have_gtk = False
    have_gtkglext = False
    have_glew = False

    gldb_env = envs['host'].Clone()
    conf = Configure(gldb_env, custom_tests = BugleChecks.tests)
    if conf.CheckPkgConfig():
        if conf.CheckPkg(aspects, 'gtk+-2.0', '2.4.0', without_name = 'without_gtk') and conf.CheckPkg(aspects, 'gthread-2.0', '2.0', without_name = 'without_gtk'):
            have_gtk = True
        if conf.CheckPkg(aspects, 'gtkglext-1.0', '1.0.0', without_name = 'without_gtkglext'):
            have_gtkglext = True
        if conf.CheckLibWithHeader('GLEW', 'GL/glew.h', 'c', autoadd = False):
            have_glew = 'GLEW'
        elif conf.CheckLibWithHeader('glew32', 'GL/glew.h', 'c', autoadd = False):
            have_glew = 'glew32'
    conf.CheckLib('m')
    gldb_env = conf.Finish()

    if not have_gtk:
        print 'WARNING: GTK+ 2.4+ is needed for the debugger. It will not be built'
        aspects['parts'].remove('debugger')
        Return()

    gldb_env.MergeFlags('!pkg-config gtk+-2.0 --cflags --libs')
    gldb_env.MergeFlags('!pkg-config gthread-2.0 --cflags --libs')
    if have_glew and have_gtkglext:
        gldb_env.MergeFlags('!pkg-config gtkglext-1.0 --cflags --libs')
        gldb_env.Append(LIBS = [have_glew])
        gldb_env.Append(CPPDEFINES = [('HAVE_GTKGLEXT', '1')])
    else:
        if not have_glew:
            print 'WARNING: GLEW was not detected. The debugger will be missing features'
        if not have_gtkglext:
            print 'WARNING: GtkGLExt was not detected. The debugger will be missing features'

    # Non-GUI files
    gldb_shared = envs['host'].StaticObject([
        'gldb-common.c',
        'gldb-channels.c'])

    gldb_gui = gldb_env.Program('gldb-gui', [
        'gldb-gui-backtrace.c',
        'gldb-gui-breakpoint.c',
        'gldb-gui-buffer.c',
        'gldb-gui-framebuffer.c',
        'gldb-gui-image.c',
        'gldb-gui-shader.c',
        'gldb-gui-state.c',
        'gldb-gui-target.c',
        'gldb-gui-texture.c',
        'gldb-gui.c',
        gldb_shared], LIBS = [targets['bugleutils'].out, '$LIBS'])

    gldb_env.Install(aspects['bindir'], gldb_gui)
